// algowala.cc
//
// by jag bot
//

#include <iostream>
#include <iomanip>
#include <iterator>
#include <fstream>
#include <string>
#include <list>
#include <algorithm>
#include <signal.h>
#include <stdlib.h>
#include "chrono.h"
#include "stock.h"
#include "ansi_color_codes.h"
#include "market_clock.h"
#include <sstream>

using namespace std;


struct name_out {
  
  ofstream& f;
  
  name_out (ofstream& _f) : f(_f) {}
  void operator() (const stock& s) {
    f << s.name << "\t";
  }
  
};

struct price_out {
  
  ofstream& f;
  int endln;
  
  price_out (ofstream& _f) : f(_f) {}
  void operator() (const stock& s) {
    f << s.price << "\t";
  }
  
};

void ctrl_c_pressed (int signum) {
  cout << endl << "ctrl + c pressed" << endl;
  exit (signum);
}

ostream& operator<< (ostream& out, const market_clock& mc) {
  out << setfill ('0') << right << setw (2) << mc.hours << ":" << setw (2) << mc.minutes << ":" << setw (2) << mc.seconds << "." << setw(3) << setprecision (0) << mc.milliseconds;
}

int main (int argc, char* argv[]) {
    
  signal (SIGINT, ctrl_c_pressed); // handle ctrl+c as we infinite loop (see below)
  
  if (argc < 5) {
    cout << "syntax: algowala scale hh mm ss" << endl;
    return 0;
  }
  
  stringstream ss; ss << argv[1] << ' ' << argv[2] << ' ' << argv [3] << ' ' << argv[4]; 
  
  // speed up (scale > 1) or slow down (0 < scale < 1) our nanosecond precise clock
  int scale; ss >> scale;
  
  // initial market time
  int h, m, s; 
  ss >> h >> m >> s;

  chrono clk (scale); // our nanosecond precise clock!
  market_clock mkt_clk (h, m, s, 0); // market clock; displays time in HH:MM:SS.mmm;
  
  double t0 = clk.secs, t1 = t0;
  clk.reset (); // get ready to tick
  mkt_clk.reset (clk);
  while (1) {
    cout << mkt_clk << "\r";
    mkt_clk.tick (clk);
    clk.tick ();
  }
  return 0;
}
